home *** CD-ROM | disk | FTP | other *** search
- Path: in2.uu.net!pipeline!not-for-mail
- From: cgister@nyc.pipeline.com (Carey Gister)
- Newsgroups: comp.lang.c++
- Subject: Re: Help: How to initialize objects created by new class[x]
- Date: 5 Jan 1996 04:52:53 -0500
- Organization: The Pipeline
- Message-ID: <4cisdl$jt3@pipe9.nyc.pipeline.com>
- References: <4cicd6$lsv@news.capitalnet.com>
- NNTP-Posting-Host: pipe9.nyc.pipeline.com
- X-PipeUser: cgister
- X-PipeHub: nyc.pipeline.com
- X-PipeGCOS: (Carey Gister)
- X-Newsreader: The Pipeline v3.4.0
-
- On Jan 05, 1996 05:19:34 in article <Help: How to initialize objects
- created by new class[x]>, 'wingl@capitalnet.com' wrote:
-
-
- >Let say I have the following class
- >
- >My_class {
- >public:
- >int (int i=0) : a(i), b(i) {}
- >private:
- >int a, b;
- >}
- >
- >For the following statement, default constructor is involved upon
- >the creation of the object.
- >
- >My_class* my_ptr= new My_class;
- >
- >However, if I use
- >My_class* my_ptr= new My_class[10];
- >
- >to allocate 10 objects of class My_class, then C++ would not
- >call any constructor to do any initialization. All the objects
- >allocated are not initializated at all.
- >
- >I wonder what is the proper way to perform object initialization
- >whenever an array of objects are created? As long as a constructor
- >is involved for each of the object in the array, I don't care if all the
- >objects are initializated the same or not.
- >
- >If there is no way to get constructor to initialize an array of objects
- >upon creation. Does that means one should not do so?
- >
- >Thanks for any help.
- >
- >Wing
- >wingl@bnr.ca
-
-
- Ahh, but a constructor IS called: the constructor of type MyClass(void)!
- and, in fact, it is
- once for each element of the array.
-
- I too am interested in the answer to your question regarding initialization
- of the objects:
- I always call an initialization method after allocating arrays this way.
- The technique is called
- "2-phase construction".
-
- Carey
- [end]
-